package com.business_clean.ui.activity.permissions;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.SizeUtils;
import com.business_clean.app.App;
import com.business_clean.app.base.BaseActivity;
import com.business_clean.app.weight.EqualSpacingItemDecoration;
import com.business_clean.data.mode.permission.PermissionData;
import com.business_clean.data.mode.permission.PermissionList;
import com.business_clean.databinding.ActivityPermissionsBinding;
import com.business_clean.ui.adapter.custom.permission.PermissionAdapter;
import com.business_clean.viewmodel.request.PermissionsViewModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

public class PermissionsActivity extends BaseActivity<PermissionsViewModel, ActivityPermissionsBinding> {

    private String channel = "0";//类型0全部 1总部员工角色 2项目成员角色
    private String selectText = "";
    private PermissionAdapter mAdapter;

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        mAdapter = new PermissionAdapter();
        mDatabind.recycler.setAdapter(mAdapter);
        mDatabind.recycler.setLayoutManager(new LinearLayoutManager(this));
        mDatabind.recycler.addItemDecoration(new EqualSpacingItemDecoration(SizeUtils.dp2px(10)));


        if (getIntent() != null && getIntent().getExtras() != null) {
            channel = getIntent().getExtras().getString("channel", "0");
            selectText = getIntent().getExtras().getString("selectText");
            //添加总部成员的时候 是 1
            mViewModel.requestPermissionsll(channel);
        } else {
            List<PermissionData> permissionData = new ArrayList<>();
            //添加总部成员的时候
            if ("1".equals(channel)) {//添加总部成员 1
                permissionData.add(new PermissionData("0", "无操作权限", "该用户仅存在于花名册中，不可登录APP进行操作"));
                permissionData.add(new PermissionData("1", "管理员", "可管辖其所属项目的成员、考勤统计、考勤设置等"));
                permissionData.add(new PermissionData("3", "人事", "可以使用待办、花名册、离职管理、考勤统计、排班表、拍照、工作圈等，不可增删查改客户、项目"));
            } else {
                permissionData.add(new PermissionData("0", "无操作权限", "该用户仅存在于花名册中，不可登录APP进行操作"));
                permissionData.add(new PermissionData("5", "保洁员", "可处理其收到的待办、拍摄水印照片、查看所属项目的工作圈和通讯录（不可查看人员详情）、查看自己的考勤结果"));
                permissionData.add(new PermissionData("4", "领班", "在保洁员的权限基础上增加领班打卡、修改团队成员的考勤结果的能力，但不能添加/删除团队成员"));
                permissionData.add(new PermissionData("2", "项目负责人", "可管辖其所属项目的成员、考勤统计、考勤设置等"));
            }
            mAdapter.setList(permissionData);
        }


        mAdapter.updateSelect(selectText);

    }


    @Override
    public void onBindViewClick() {
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                App.appViewModelInstance.getSelectPermissionsStatus().setValue(mAdapter.getData().get(position));
                finish();
            }
        });
    }

    @Override
    public void onRequestSuccess() {
        //监听获取结果
        mViewModel.getPermission().observe(this, new Observer<PermissionList>() {
            @Override
            public void onChanged(PermissionList permissionList) {
                permissionList.getList().add(0, new PermissionData("0", "无操作权限", "该用户仅存在于花名册中，不可登录APP进行操作"));
                mAdapter.setList(permissionList.getList());
            }
        });
    }
}